package com.rc.evcharger.common.utils.elec;


import com.rc.evcharger.common.utils.DateHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateTransformUtil {

    //峰谷平
    public static String[][] fgps = {{"08:00-11:00", "18:00-23:00"}, {"23:00-7:00"}, {"07:00-08:00", "11:00-18:00"}};

    public static Map<String, String> fgpMap = new HashMap();

    static {
        fgpMap.put("00:00:00-07:59:59", "低谷");

        fgpMap.put("14:00:00-16:59:59", "高峰");
        fgpMap.put("19:00:00-21:59:59", "高峰");

        fgpMap.put("08:00:00-13:59:59", "平段");
        fgpMap.put("17:00:00-18:59:59", "平段");
        fgpMap.put("22:00:00-23:59:59", "平段");
    }

    public static String getCodeByDate(Date startTime) {

        String start = DateHelper.dataToString(startTime, DateHelper.DATE_FORMAT_HHMMSS);

        for (String s : fgpMap.keySet()) {
            String[] split = s.split("-");
            if (start.compareTo(split[0]) >= 0 && start.compareTo(split[1]) <= 0) {
                return fgpMap.get(s);
            }
        }

        return "未知状态";
    }


    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String codeHandle(Date codeTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date begin = new Date();
        Date end = new Date();
        int flag = 1;
        int destory = 0;
        for (int i = 0; i < fgps.length; i++) {
            String[] fgp = fgps[i];
            for (int j = 0; j < fgp.length; j++) {
                String[] split = fgp[j].split("-");
                for (int k = 0; k < split.length; k++) {
                    if (k == 0) {
                        begin = format.parse(split[k]);
                    } else {
                        end = format.parse(split[k]);
                    }
                }
                if (codeTime.getTime() >= begin.getTime() && codeTime.getTime() < end.getTime()) {
                    flag = i;
                    destory = 1;
                    break;
                }
            }
            if (destory == 1) {
                break;
            }
        }

        begin = format.parse("23:00");
        end = format.parse("07:00");
        if (codeTime.getTime() >= begin.getTime() || codeTime.getTime() < end.getTime()) {
            return "低谷";
        }
        if (flag == 0) {
            return "高峰";
        } else if (flag == 1) {
            return "低谷";
        } else {
            return "平段";
        }
    }

    public static void main(String[] args) {
        //SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        //try {
        //    String s = codeHandle(format.parse("07:30"));
        //    System.out.println(s);
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}

        String codeByDate = getCodeByDate(new Date());
        System.out.println(codeByDate);
    }
}
